﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/RoleInfo")]
    public class RoleInfoController : ApiController
    {
        private RoleInfoBLL bll = new RoleInfoBLL();
        [Route("Query")]
        [HttpGet]
      
        public Result Get(string RoleName="",int PageSize=10,int PageIndex=1)
        {
            int total = 0;
            var data = bll.Query(RoleName, out total, PageSize, PageIndex);
            return Result.Success(new { data,total }) ;
        }
        [Route("Add")]
        [HttpPost]
        public Result Add(RoleInfo entity) 
        {
            entity.IsDelete = 0;
            if (bll.Add(entity))
            {
                
                return Result.Success();
            }else
            {
                return Result.Error("添加失败"); 
            }
        }
        public Result Updata(RoleInfo entity) 
        {
            if (entity.Id==0) 
            {
                return Result.Error("Id不能为空");
            }else 
            {
            RoleInfo roleInfo =bll.GetOne(entity.Id);

                roleInfo.RoleName = entity.RoleName; 
                roleInfo.Remark = entity.Remark;
                roleInfo.IsDelete = entity.IsDelete;

               bool num=bll.Update(roleInfo);
                if  (num) 
                {
                return Result.Success();
                }else 
                {
                    return Result.Error("修改失败，仔细请查看字符是否出错");
                }
            }
        }

        [Route("Delete")]
        [HttpPost]
        public Result Delete(int  Id) 
        {
          RoleInfo roleInfo =bll.GetOne(Id);
          roleInfo.IsDelete = 1;
            bool num = bll.Update(roleInfo);
            if (num)
            {
                return Result.Success();
            }
            else 
            {
                return Result.Error("删除失败");
            }
        }

        [Route("Get")]
        [HttpGet]
        public Result Gei(int Id) 
        {
            return Result.Success( bll.GetOne(Id));
        }

        [Route("GetAll")]
        [HttpGet]
        public Result GetAll()
        {
            return Result.Success(bll.Getall());
        }

    }
}